﻿#define DEBUGMODE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using SpaceBattle.Graphics;
using SpaceBattle.Utilities;

namespace SpaceBattle.Viev
{
    public class Sprite : DrawableGameComponent
    {
        protected Texture2D texture;
        protected float scale = 1;
        protected float rotation;
        protected SpriteFont font;
        protected Color color;
        protected Vector2 position;

        public Texture2D Texture
        {
            get
            {
                return texture;
            }
        }

        public float Rotation
        {
            get
            {
                return MathHelper.ToDegrees(rotation);
            }
            set
            {
                float rot = value;
                if (rot<0)
                {
                    rot += 360;
                }
                rotation = MathHelper.ToRadians(rot) % (float)(2 * Math.PI);
            }
        }

        public float Scale
        {
            get
            {
                return scale;
            }
            set
            {
                scale = MathHelper.Clamp(value, 0, 10);
            }
        }

        public Sprite(Texture2D texture, float scale, float rotation, Color color, Vector2 position)
            : base(GameMain.Game())
        {
            GameMain.Game().Components.Add(this);
            this.texture = texture;
            Scale = scale;
            Rotation = rotation;
            this.color = color;
            this.position = position;
        }

        public override void Draw(GameTime gameTime)
        {
            GameMain.Game().CameraBatch.Draw(texture, position, new Rectangle(0, 0, texture.Width, texture.Height),
                color, rotation, new Vector2(texture.Width / 2, texture.Height / 2), Scale, SpriteEffects.None, 0);
        }

        protected override void LoadContent()
        {
            base.LoadContent();
            font = GameMain.Game().Content.Load<SpriteFont>("myFont");
        }
    }

    public class PhysicsSprite : Sprite
    {
        private Body body;
        private ForceVector forceVector;

        public Body Body
        {
            get { return body; }
        }

        public PhysicsSprite(Body body, Boolean solid, Texture2D texture)
            : base(texture, body.Size/texture.Width, body.Rotation, body.Color, body.Position)
        {
            this.body = body;
            this.body.Solid = solid;
            GameMain.Game().addPhysicBody(body);

            //forceVector = new ForceVector(body.Position, body.Position);
        }

        public override void Draw(GameTime gameTime)
        {
            GameMain.Game().CameraBatch.Draw(texture, body.Position, new Rectangle(0, 0, texture.Width, texture.Height),
                color, rotation, new Vector2(texture.Width / 2, texture.Height / 2), Scale, SpriteEffects.None, 0);

            GameMain.Game().CameraBatch.DrawString(font, "mass:" + body.Mass +
                "\npos:" + body.Position +
                "\nsize:" + new Vector2(texture.Width, texture.Height) * Scale +
                "\nscale:" + Scale +
                "\nsolid:" + body.Solid +
                "\nrotation:" + Rotation,
                body.Position, Color.White);
        }


        public override void Update(GameTime gameTime)
        {
            //forceVector.ChangePosition(body.Position, body.Position + body.Force * 100);
            Rotation = body.Rotation;
        }

        class ForceVector : DrawableGameComponent
        {
            static BasicEffect basicEffect;
            VertexPositionColor[] vertices;

            public ForceVector(Vector2 start, Vector2 end)
                : base(GameMain.Game())
            {
                GameMain.Game().Components.Add(this);

                vertices = new VertexPositionColor[2];
                vertices[0].Position = new Vector3(start, 0);
                vertices[0].Color = Color.Blue;
                vertices[1].Position = new Vector3(end, 0);
                vertices[1].Color = Color.Red;
            }

            static ForceVector()
            {
                GraphicsDeviceManager graphics = GameMain.Game().Graphics;

                basicEffect = new BasicEffect(graphics.GraphicsDevice);
                basicEffect.VertexColorEnabled = true;
                basicEffect.Projection = Matrix.CreateOrthographicOffCenter
                   (0, graphics.GraphicsDevice.Viewport.Width,     // left, right
                    graphics.GraphicsDevice.Viewport.Height, 0,    // bottom, top
                    0, 1);                                         // near, far plane
            }

            public override void Draw(GameTime gameTime)
            {
                basicEffect.CurrentTechnique.Passes[0].Apply();
                GameMain.Game().Graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, vertices, 0, 1);
            }

            public void ChangePosition(Vector2 start, Vector2 end)
            {
                vertices[0].Position = new Vector3(start, 0);
                vertices[1].Position = new Vector3(end, 0);
            }
        }
    }
}
